import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import javax.xml.bind.Element;
import java.util.concurrent.TimeUnit;

public class ChatCases extends InitAndEnd{
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void loginSuccess(String username, String password) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.get("http://43.143.18.198:8080/login.html");
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(3000);
        Alert alert = webDriver.switchTo().alert();
        Assertions.assertEquals("登陆成功!", alert.getText());
    }

    @ParameterizedTest
    @CsvFileSource(resources = "LoginFail.csv")
    void loginFail(String username, String password) throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.get("http://43.143.18.198:8080/login.html");
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(3000);
        Alert alert = webDriver.switchTo().alert();
        Assertions.assertEquals("登陆失败！", alert.getText());
    }

    @Test
    void getUserInfo() throws InterruptedException {
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Login.login("hans", "123");
        Thread.sleep(3000);
        WebElement element = webDriver.findElement(By.cssSelector("body > div.client_container > div > div.left > div.user"));
        Assertions.assertEquals("hans", element.getText());
    }
}
